export class tableStore {
  columns: Array<any> = []
  data: any = []
  
  init(initialState: any) {
    Object.keys(initialState).forEach(key => {
      (this as any)[key] = initialState[key];
    });
    if (!Array.isArray(this.columns)) {
      this.columns = []
    }

    if (!Array.isArray(this.data)) {
      this.data = []
    }
  }

  width = 0;
  contentWidth = 0;
  colWidths: Array<number> = []
  updateWidth(value: any) {
    console.log("updateWidth", this, value)
    this.width = Math.floor(value)
    let colWidths: Array<number> = []
    let lastWidth = this.width
    if (Array.isArray(this.columns)) {
      colWidths = Array.from({ length: this.columns.length })
      const customColumn: Array<number> = []
      colWidths.forEach((v, index) => {
        const column = this.columns[index]
        if (typeof column.width === "number") {
          colWidths[index] = column.width
          lastWidth -= colWidths[index]
        } else {
          customColumn.push(index)
        }
      })
      const divide = Math.floor(lastWidth / customColumn.length)
      customColumn.forEach(index => {
        if (index === customColumn.length - 1) {
          colWidths[index] = lastWidth
        } else {
          colWidths[index] = divide
          lastWidth = Math.max(lastWidth - divide, 30)
        }
      })
    }
    this.colWidths = colWidths
    const maxWidth = this.colWidths.reduce((acc, cur) => acc + cur, 0)
    this.contentWidth = Math.max(maxWidth, this.width)
  }

  bodyHeight = 0
  updateBodyHeight(value: number) {
    this.bodyHeight = value
  }

  scrollbarWidth = 0
  updateScrollbarWidth(value: number) {
    this.scrollbarWidth = value
  }

  update(data: any) {
    if (Array.isArray(data)) {
      this.data = data
    }
  }

}
